class Board:
    def __init__(self, size=15):
        self.size = size
        self.board = [[0 for _ in range(size)] for _ in range(size)]
        self.history = []
        self.last_move = None

    def place_stone(self, row, col, player):
        if not self.is_valid_move(row, col):
            return False

        self.board[row][col] = player
        self.history.append((row, col, player))
        self.last_move = (row, col)
        return True

    def is_valid_move(self, row, col):
        if row < 0 or row >= self.size or col < 0 or col >= self.size:
            return False
        return self.board[row][col] == 0

    def undo(self):
        if not self.history:
            return False

        row, col, _ = self.history.pop()
        self.board[row][col] = 0

        if self.history:
            self.last_move = (self.history[-1][0], self.history[-1][1])
        else:
            self.last_move = None

        return True

    def check_winner(self, row, col):
        player = self.board[row][col]
        if player == 0:
            return False

        directions = [
            (0, 1),
            (1, 0),
            (1, 1),
            (1, -1)
        ]

        for dr, dc in directions:
            count = 1

            r, c = row + dr, col + dc
            while 0 <= r < self.size and 0 <= c < self.size and self.board[r][c] == player:
                count += 1
                r += dr
                c += dc

            r, c = row - dr, col - dc
            while 0 <= r < self.size and 0 <= c < self.size and self.board[r][c] == player:
                count += 1
                r -= dr
                c -= dc

            if count >= 5:
                return True

        return False

    def reset(self):
        self.board = [[0 for _ in range(self.size)] for _ in range(self.size)]
        self.history = []
        self.last_move = None

    def get_stone(self, row, col):
        return self.board[row][col]

    def clone(self):
        new_board = Board(self.size)
        new_board.board = [row[:] for row in self.board]
        new_board.history = list(self.history)
        new_board.last_move = self.last_move
        return new_board
